package com.breadenglish.cms.session.web;

import com.breadenglish.cms.session.domain.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 作者: guo yao
 * 日期: 2016年02月14日
 * 时间: 下午19:10
 */
@Controller
@RequestMapping("/session")
public class SessionController {

    private static Logger log = LoggerFactory.getLogger(SessionController.class);

    @RequestMapping("/main.html")
    public String main() {
        System.out.println("方法");
        System.out.println("=====方法======");
        System.out.println("=====方法======");
        System.out.println("======方法=====");
        log.trace("======trace");
        log.debug("======debug");
        log.info("======info");
        log.warn("======warn");
        log.error("======error");
        return "Hello World! Web";
    }

    /**
     * 加入我们页面[英文]
     *
     * @param request
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/index.html")
    public ModelAndView indexSession(HttpServletRequest request) throws Throwable {
        ModelAndView mav = new ModelAndView();
        request.getSession().invalidate();
        mav.setViewName("session/index");
        return mav;
    }

    // 设置session
    @RequestMapping(value = "/setSessoin.html")
    public ModelAndView setSessoin(HttpServletRequest request) throws Throwable {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = new UserInfo();

        String userName = request.getParameter("userName");
        String email = request.getParameter("email");
        String local = request.getLocalAddr().toString();
        String remote = request.getRemoteAddr().toString();
        request.getSession().setAttribute("userName", userName);
        request.getSession().setAttribute("email", email);
        userInfo.setName(userName);
        userInfo.setPass(email);
        request.getSession().setAttribute("userInfo", userInfo);

        mav.addObject("userName", userName);
        mav.addObject("email", email);
        mav.addObject("local", local);
        mav.addObject("remote", remote);
        mav.addObject("port", request.getServerPort());
        mav.addObject("sessionId", request.getSession().getId());
        mav.setViewName("session/show");
        return mav;
    }

    // 浏览session
    @RequestMapping(value = "/getSession.html")
    public ModelAndView getSession(HttpServletRequest request) throws Throwable {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = new UserInfo();
        String local = request.getLocalAddr().toString();
        String remote = request.getRemoteAddr().toString();
        Object userName = request.getSession().getAttribute("userName");
        Object email = request.getSession().getAttribute("email");
        if (userName != null) {
            userInfo.setName(userName.toString());
        }
        if (email != null) {
            userInfo.setPass(email.toString());
        }
        request.getSession().setAttribute("userInfo", userInfo);
        mav.addObject("userName", userName);
        mav.addObject("email", email);
        mav.addObject("local", local);
        mav.addObject("remote", remote);
        mav.addObject("port", request.getServerPort());
        mav.addObject("sessionId", request.getSession().getId());
        mav.setViewName("session/show");
        return mav;
    }
}
